package common.util;

import cn.hutool.core.exceptions.ExceptionUtil;
import cn.hutool.core.util.ReflectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import common.config.exception.CustomException;

import java.util.Map;

public class WrapperBuilder {

	private WrapperBuilder() {
		throw new AssertionError("不可实例化");
	}

	public static <T> QueryWrapper<T> buildWrapperWithMap(Map<?, ?> map, Class<T> clazz) {
		T entity;
		try {
			entity = clazz.newInstance();
		} catch (InstantiationException | IllegalAccessException e) {
			e.printStackTrace();
			throw new CustomException(ExceptionUtil.getSimpleMessage(e));
		}
		for (Map.Entry<?, ?> entry : map.entrySet()) {
			ReflectUtil.setFieldValue(entity, StrUtil.toCamelCase(String.valueOf(entry.getKey())), entry.getValue());
		}
		return new QueryWrapper<>(entity);
	}

}
